using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using WindowsFormsApplication1;
using Sim;
using Lists;
using Sim.Lists;
using System.Net;

namespace ClientApplication
{
    public class ClientApplications
    {

        #region Properties
        ASCIIEncoding byteEncode = new ASCIIEncoding();
        static public string serverIP { get; set; }
        IPEndPoint rxPoint;
        UdpClient udpTransmit = new UdpClient(25001);
        UdpClient udpReceive = new UdpClient(25002);
        string myIP = IPList.GetPublicIP();

        public bool connected { get; set; }
        private byte[] byteMsgSend;
        private byte[] byteMsgReceive;
        private string receivedMsg;
        #endregion

        public string receiveMsg()
        {
            byteMsgReceive = udpReceive.Receive(ref rxPoint);
            return byteEncode.GetString(byteMsgReceive);
        }
        public void transmitMsg(string msg)
        {
            byteMsgSend = byteEncode.GetBytes(msg);
            udpTransmit.Send(byteMsgSend, byteMsgSend.Length, serverIP, 25001);
        }

        public bool checkAck(string[] ack)
        {
            if (ack[2] == "Success")
            {
                transmitMsg(myIP + "#" + ack[1] + " Success");
                return true;
            }
            else
            {
                transmitMsg(myIP + "#" + ack[1] + " Failed");
                return false;
            }
        }

        public void Receive()
        {
            while (true)
            {
                receivedMsg = receiveMsg();
                string[] ackMsg = receivedMsg.Split('#');

                if (ackMsg[1] == "ServerBroadcast" && !connected)
                {
                    serverIP = ackMsg[0];
                    connected = true;
                    rxPoint = new IPEndPoint(IPAddress.Parse(serverIP), 25002);
                    Console.WriteLine(receivedMsg);
                    transmitMsg(myIP + "#Connected");
                }

                if (connected)
                {
                    if (ackMsg[1] == "Login")
                    {
                        Program.isLoggedIn = checkAck(ackMsg);
                        Console.WriteLine(receivedMsg);
                    }
                    else if (ackMsg[1] == "Registration")
                    {
                        checkAck(ackMsg);
                    }
                    else if (ackMsg[1] == "Sendscore")
                    {
                        checkAck(ackMsg);
                    }
                    else if (ackMsg[1] == "SendDive")
                    {
                        checkAck(ackMsg);
                    }
                    else if (ackMsg[1] == "Refresh")
                    {
                        checkAck(ackMsg);
                        Console.WriteLine(receivedMsg);
                        Program.tournamentInfo tourinfo = new Program.tournamentInfo();
                        tourinfo.title = ackMsg[2];
                        tourinfo.date = ackMsg[3];
                        tourinfo.gender = ackMsg[4];
                        for (int i = 0; i < Program.ti.Count; i++)
                        {
                            if (Program.ti[i].title == ackMsg[2])
                                return;
                        }
                        Program.ti.Add(tourinfo);
                        
                    }
                }
            }
        }

        public void registerUser()
        {
            Console.WriteLine("Input First name: ");
            string name = Console.ReadLine();
            Console.WriteLine("Input Last name: ");
            string lastname = Console.ReadLine();
            Console.WriteLine("Input Age: ");
            int age = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Input Nationality: ");
            string nat = Console.ReadLine();
            Console.WriteLine("Input Gender: ");
            string sex = Console.ReadLine();
            Console.WriteLine("Input Level: ");
            int level = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Input Username: ");
            string username = Console.ReadLine();
            Console.WriteLine("Input Password: ");
            string password = Console.ReadLine();
            transmitMsg(myIP + "#RegisterUser#" + name + "#" + lastname + "#" + age + "#" + nat + "#" + sex + "#" + level + "#" + username + "#" + password);
        }

        public void sendLogin(string username, string password)
        {
            transmitMsg(myIP + "#Login#" + username + "#" + password);
        }

        public void sendRefreshRequest()
        {
            transmitMsg(myIP + "#Refresh");
        }

        //======JUDGE==========
        public void sendJudgeScore()
        {
            Console.WriteLine("===Send score===");
            Console.WriteLine("Input jumpscore: ");
            String jumpscore = Console.ReadLine();

            transmitMsg(myIP + "#Sendscore#" + jumpscore);
        }

        //=====DIVER===========
        public void sendDive()
        {
            Console.WriteLine("Send Dive\n");
            Console.WriteLine("Input Divecode: ");
            String diveCode = Console.ReadLine();
            transmitMsg(myIP + "#SendDive#" + diveCode);
        }
    }
}

